<template>
    <!-- use the modal component, pass in the prop -->
    <modal v-if="stateManager.SeeDialogVisible"
           @cancel="closeDialog"
           @ok="saveModel"
           :modal-title="modalTitle"
           model-size="normal">
        <!--
          you can use custom content here to overwrite
          default content
        -->
        <vue-form>
            <div class="row">
                <div class="col-xs-12">
                    <vue-form-item label="备注信息" :error-message="model.errors.remark || errors.first('remark')">
                        <input-text v-model:value="model.remark"
                                    v-validate="'max:15'"
                                    label="备注信息"
                                    el-name="remark"></input-text>
                    </vue-form-item>
                </div>
            </div>
        </vue-form>
    </modal>


</template>

<script>
    export default {
        props: {
            stateManager: {
                type: Object
            }
        },
        data: function () {
            return {
                model: this.$modelDataSource({
                    url: 'api/WeChat/WeChatUser',
                    dataKey: 'model',
                    attributes: {
                        id:0,
                        open_id:'',
                        remark:'',
                    }
                })
            };
        },
        methods: {
            closeDialog: function () {
                this.stateManager.SeeDialogVisible = false;
                this.stateManager.SeeModelId = 0;
            },
            saveModel: function () {
//                ladda.start();
                let self = this;
                    if(self.validate){
                    let formData = new FormData();
                    formData.append('id', this.model.id);
                    formData.append('remark', this.model.remark);
                    let config = {
                        headers: {
                            'Content-Type': 'multipart/form-data'
                        }
                    }
                    axios.post('/wechat/update-user-remark', formData, config).then(function (res) {
                        self.stateManager.refreshList = true;
                        self.closeDialog();
                        toastr.success('修改成功！');
                    }).catch(function (res) {
                        console.log(res);
                        self.stateManager.refreshList = true;
                        toastr.error('修改失败');
                    })
                }
            },
            validate: function (successCallback, failedCallback) {
                this.$validator.validateAll().then(() => {
                    // eslint-disable-next-line
                    console.log('验证通过');
                    typeof successCallback === 'function' && successCallback();
                }).catch(() => {
                    // eslint-disable-next-line
                    console.log('验证未通过');
                    typeof failedCallback === 'function' && failedCallback();
                });

            }


        },
        computed: {
            modalTitle: function () {
                return '修改备注';
            }
        },
        watch: {
            'stateManager.SeeModelId': function (newValue) {
                this.model.fetch(newValue);
                console.log(this.model)
            }
        }
    }
</script>